/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
// SYSTEM INCLUDES
#include <fbs.h>

// USER INCLUDES
#include "bitmapmethods.h"
#include <bitstd.h>
#include <bitdev.h>
#include <eikenv.h>
#include "FadeProvider.h"


//////////////////////////////////////////////////////////////////////////
CFadeProvider* CFadeProvider::NewL()
{
    CFadeProvider* self = CFadeProvider::NewLC();
    CleanupStack::Pop(self);
    return self;
}

//////////////////////////////////////////////////////////////////////////
CFadeProvider* CFadeProvider::NewLC()
{
    CFadeProvider* self = new (ELeave) CFadeProvider();
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

//////////////////////////////////////////////////////////////////////////
CFadeProvider::CFadeProvider()
{
    // No implementation required
}

//////////////////////////////////////////////////////////////////////////
CFadeProvider::~CFadeProvider()
{
    delete iTransitionBitmapGc;
    delete iTransitionBitmapDevice;
    delete iTransitionBitmap;
    delete iTimer;
}

//////////////////////////////////////////////////////////////////////////
void CFadeProvider::ConstructL()
{
    iTimer = CIdle::NewL(CIdle::EPriorityIdle);
    // Defaults to white color
    iTargetRgb = KRgbWhite;
    // Defaults to fade in
}

//////////////////////////////////////////////////////////////////////////
void CFadeProvider::SetupTransition(CFbsBitmap* aFirst, CFbsBitmap* /*aSecond*/, TUint aTransitionTime)
{
    iSourceBitmap = aFirst;

    delete iTransitionBitmap;
    iTransitionBitmap = NULL;

    if (!iSourceBitmap)
        return;

    iTransitionBitmap = new (ELeave) CFbsBitmap();
    iTransitionBitmap->Create(iSourceBitmap->SizeInPixels(), CEikonEnv::Static()->ScreenDevice()->DisplayMode());
    iTransitionBitmapDevice = NBitmapMethods::CreateBitmapDeviceL(*iTransitionBitmap);
    iTransitionBitmapGc = NBitmapMethods::CreateGraphicsContextL(*iTransitionBitmapDevice);
    iTransitionBitmapGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
    iTransitionBitmapGc->SetBrushColor(KRgbWhite);
    iTransitionBitmapGc->Clear();
    iFadeColor = KRgbWhite;
    iTransitionTime = aTransitionTime;
}

//////////////////////////////////////////////////////////////////////////
void CFadeProvider::SetFadingColor(TRgb& aRgb)
{
    iFadeColor = aRgb;
}

//////////////////////////////////////////////////////////////////////////
void CFadeProvider::SetFadeDirection(TBool aFadeIn)
{
    iFadeIn = aFadeIn;
}

//////////////////////////////////////////////////////////////////////////
TBool CFadeProvider::FadeDirection() const
{
    return iFadeIn;
}

//////////////////////////////////////////////////////////////////////////
void CFadeProvider::BeginTransition()
{
    if (iSourceBitmap)
    {
        iTimer->Start(TCallBack(IdleCallback, this));
        iTimeUtils.Reset();
    }
}

//////////////////////////////////////////////////////////////////////////
void CFadeProvider::StopTransition()
{
    iTimer->Cancel();
}

//////////////////////////////////////////////////////////////////////////
CFbsBitmap* CFadeProvider::TransitionBitmap() const
{
    return iTransitionBitmap;
}

//////////////////////////////////////////////////////////////////////////
TInt CFadeProvider::IdleCallback(TAny* aPtr)
{
    CFadeProvider* self = (CFadeProvider*) aPtr;
    return self->TransitionStep();
}

//////////////////////////////////////////////////////////////////////////
TInt CFadeProvider::TransitionStep()
{

    if (iTimeUtils.GetTime() >= iTransitionTime)
    {
        TransitionCompleted();
        return 0;
    }
    TInt alpha = iFadeIn ? (255 - (iTimeUtils.GetTime()* 255) / iTransitionTime) : ((iTimeUtils.GetTime()* 255) / iTransitionTime);
    if (alpha > 255)
        alpha = 255;
    if (alpha < 0)
        alpha = 0;
    iTransitionBitmapGc->BitBlt(TPoint(0, 0), iSourceBitmap);
    iFadeColor.SetAlpha(alpha);
    iTransitionBitmapGc->SetBrushColor(iFadeColor);
    iTransitionBitmapGc->SetPenColor(iFadeColor);
    iTransitionBitmapGc->DrawRect(TRect(TPoint(0, 0), iTransitionBitmap->SizeInPixels()));

    StepCompleted();
    return 1;
}
